package ng.lib;

import java.awt.geom.Point2D;

public class Vector2D extends Point2D.Float {
	
	public Vector2D(float x, float y) {
		this.x = x;
		this.y = y;
	}

	public Vector2D() {
		this.x = 0;
		this.y = 0;
	}
	
	public Vector2D contractTo(double v) {
		double f = v/this.getValue();
		this.x *= f;
		this.y *= f;
		return this;
	}

	public Vector2D add(Vector2D v) {
		this.x += v.x;
		this.y += v.y;
		return this;
	}

	public Vector2D multiply(float f) {
		this.x *= f;
		this.y *= f;
		return this;
	}
	
	public Vector2D rotate(double a) {
		float tmpX = this.x;
		this.x = (float) Math.cos(Math.atan2(this.y, this.x) + a);
		this.y = (float) Math.sin(Math.atan2(this.y, tmpX) + a);
		return this;
	}

	public double getValue() {
		return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));

	}


	public double getAngle() {
		double v = this.getValue();
		if (v == 0)
			return 0;
		else if (y > 0)
			return Math.acos(this.x / this.getValue());
		else
			return Math.PI * 2 - Math.acos(this.x / this.getValue());

	}

	public Vector2D clone() {
		return new Vector2D(this.x, this.y);
	}
	
	public static Vector2D add(Vector2D v1, Vector2D v2){
		return new Vector2D(v1.x+v2.x,v1.y+v2.y);
	}

}
